﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Security.Cryptography;

/// <summary>
/// Summary description for XuLy
/// </summary>
public class XuLy
{
    SqlCommand command;
    SqlDataAdapter adapter;
    DataSet ds;
    //DataTable dt;
    KetNoi kn = new KetNoi();
    public DataTable Load(string query)
    {
        kn.MoKN();
        adapter = new SqlDataAdapter(query, kn.connection);
        ds = new DataSet();
        adapter.Fill(ds);
        kn.DongKN();
        return ds.Tables[0];
    }
    public void XuLyDuLieu(string query)
    {
        kn.MoKN();
        command = new SqlCommand(query, kn.connection);
        command.ExecuteNonQuery();
        kn.DongKN();
    }

    // tao so san pham tren 1 trang 
    public DataTable GetDataTable(string query, int start, int length)
    {
        SqlDataAdapter adapter = new SqlDataAdapter(query, kn.connection);
        DataTable dataTable = new DataTable();
        adapter.Fill(start, length, dataTable);
        return dataTable;
    }

    //public DataTable TaoGioHang()
    //{
    //    DataTable dataTable = new DataTable();
    //    dataTable.Columns.Add("MaSP");
    //    dataTable.Columns.Add("TenSP");
    //    dataTable.Columns.Add("DonGia");
    //    dataTable.Columns.Add("SoLuongSP");
    //    dataTable.Columns.Add("TongTien");
    //    return dataTable;
    //}
    
    //code phan trang cho san pham 
   




    // chua dang nhap van có thể mua hang 
    public DataTable GioHang()
    {
        DataTable dttable = new DataTable();
        dttable.Columns.Add("MaSP", Type.GetType("System.String"));
        dttable.Columns.Add("TenSp", Type.GetType("System.String"));
        dttable.Columns.Add("Gia", Type.GetType("System.String"));
        dttable.Columns.Add("SoLuong", Type.GetType("System.String"));
        dttable.Columns.Add("ThanhTien", Type.GetType("System.String"));
        dttable.Columns.Add("NgayDat", Type.GetType("System.String"));
        dttable.Columns[0].AllowDBNull = false;
        dttable.PrimaryKey = new DataColumn[] {dttable.Columns[0] };
        return dttable;
    }
    /*
    DataTable dt;
    public DataTable Giohang()
    {
        dt = new DataTable();
       // dt.Columns.Add("TenDN", Type.GetType("System.String"));
        dt.Columns.Add("MaSP", Type.GetType("System.String"));
        dt.Columns.Add("TenSp", Type.GetType("System.String"));
        dt.Columns.Add("Gia", Type.GetType("System.String"));
        dt.Columns.Add("SoLuong", Type.GetType("System.String"));
        dt.Columns.Add("ThanhTien", Type.GetType("System.String"));
        dt.Columns.Add("NgayDat", Type.GetType("System.String"));
        dt.Columns.Add("TenDN", Type.GetType("System.String"));
        dt.Columns[0].AllowDBNull = false;
        dt.PrimaryKey = new DataColumn[] { dt.Columns[1], dt.Columns[0] };
        return dt;
    }*/
    // kiem tra co san pham trong gio hang hay chua
    //public int IsExistingItem(DataTable dt, string masp)
    //{
    //    for (int i = 0; i < dt.Rows.Count; i++)
    //    {
    //        if (dt.Rows[i][0].ToString() == masp)
    //        {
    //            return i;
    //        }
    //    }
    //    return -1;
    //}

    ////kiem tra xem san pham con hay ko?
    //public int IsHasItem(string maSP)
    //{
    //    KetNoi kn= new KetNoi();
    //    DataTable dt = new DataTable();
    //    string query;
    //    int tongSLMua;
    //    query = string.Format("select count(*) from ChiTietDonHang where MaSP='{0}'", maSP);
    //    if (kn.ExecuteScalar(query) > 0)//kiem tra xem sp nay co dc dat mua bao gio chua?
    //    {
    //        //neu sp nay da tung dc dat mua trc do
    //        query = string.Format("select sum(SoLuongMua) from tblChiTietHoaDon where MaSP='{0}'", maSP);
    //        //lay ra tong sp da dc mua
    //        tongSLMua = kn.ExecuteScalar(query);
    //    }
    //    else
    //    {
    //        //neu sp nay chua dc dat mua trc do thi tra ra la 0
    //        tongSLMua = 0;
    //    }
    //    //lay ra tong sp hien co 
    //    query = string.Format("select sum(SoLuongSP) from SanPham where MaSP='{0}'", maSP);
    //    int tongSLSP = kn.ExecuteScalar(query);
    //    //tinh so luong sp con lai trong kho
    //    int soLuongTon = tongSLSP - tongSLMua;
    //    return soLuongTon;
    //}
    

    
}